// pages/book/list/index.js
let id
let p = 0  //这种变量会被小程序缓存
Page({

    /**
     * 页面的初始数据
     */
    data: {
        booklist:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        id = options.id || '62eb731fb589d71e49d14747'
        p=0
        console.log(11111);
        this.fetchData()
    },
    handleDetail(ev){
        let {item} = ev.currentTarget.dataset
        wx.setStorage({
            key:'bookdetail',
            data:item,
            success:()=>{
                wx.navigateTo({
                  url: '/pages/book/detail/index',
                })
            }
        })
    },
    onReachBottom() {
        console.log('触底了');
        this.fetchData()
    },
    onPullDownRefresh(){
        console.log('下拉刷新');
        p=0
        this.setData({booklist:[]})
        this.fetchData()
    },
    fetchData(){
        let skip = p*12
        getApp().fly.get(`/classes/books?where={"cateid":"${id}"}&limit=12&skip=${skip}`).then(res=>{
            wx.stopPullDownRefresh() //关闭下拉刷新动画
            let {results} = res.data
            if(results.length){
                this.setData({
                    booklist:[
                        ...this.data.booklist, //原数据
                        ...results //新数据
                    ]
                })
                p++
            }else{
                wx.showToast({
                  title: '我是有底线的~',
                  icon:'none'
                })
            }
        })
    },
})